﻿using System.Security.Cryptography;

namespace Ulid;

/// <summary>
/// 用于 ULID 随机部分的一种在密码学上安全的随机数生成器
/// </summary>
public class CSUlidRng : BaseUlidRng
{
    public override byte[] GetRandomBytes(DateTimeOffset dateTime)
        => RandomNumberGenerator.GetBytes(RANDLEN);

    public override void GetRandomBytes(Span<byte> buffer, DateTimeOffset dateTime)
    {
        if (buffer.Length < RANDLEN)
        {
            Throw(buffer.Length);
            static void Throw(int len) => throw new ArgumentException($"The given buffer must be at least {RANDLEN} bytes long, actual: {len}");
        }

        RandomNumberGenerator.Fill(buffer);
    }
}
